<?php

namespace app\modules\setting\controllers;

use app\models\HsptLabel;
use app\models\LogLut;
use app\modules\clinical\models\Patient;
use app\modules\setting\models\UserPermissions;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\web\ForbiddenHttpException;

/**
 * SettingController implements the CRUD actions for SamplePatient model.
 */
class MiscellaneousController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }


    /**
     * @return string
     * @throws \yii\web\ForbiddenHttpException
     * @author sally
     * @date   2020-06-28
     */
    public function actionIndex()
    {
        $permission_flags = $this->_get_permission_flags();

        if (empty($permission_flags) or empty($permission_flags['miscellaneous'])) {
            throw new ForbiddenHttpException('没有访问权限');
        }

        return $this->render('index', [
            'permission_flags' => $permission_flags,
        ]);
    }


    /**
     * Delete labels that are not being used
     *
     * @return string
     * @throws \yii\web\ForbiddenHttpException
     * @author sally
     * @date   2020-06-28
     */
    public function actionDeleteExtraLabels()
    {
        $permission_flags = $this->_get_permission_flags();

        if (empty($permission_flags) or empty($permission_flags['delete_extra_labels'])) {
            throw new ForbiddenHttpException('没有访问权限');
        }

        $labels = Patient::find()->distinct()->select('pt_label')->where(['delete_at' => 0])->all();

        $labels = array_column($labels, 'pt_label');

        $labels_tmp = [];
        foreach ($labels as $label) {
            if (empty($label)) continue;

            $label = explode('+', $label);

            $labels_tmp = array_merge($labels_tmp, $label);
        }

        $labels = $labels_tmp;
        $labels = array_unique($labels);

        $response = HsptLabel::find()->where(['not in', 'id', $labels])->all();
        if (!empty($response)) {
            $response = array_column($response, 'name');
            $response = implode(', ', $response);
            $response = "标签[{$response}]已被删除";

            HsptLabel::deleteAll(['not in', 'id', $labels]);

            // log
            $log = new LogLut();
            $log->action = 'delete_extra_labels';
            $log->response = $response;
            $log->save();
        } else {
            $response = "没有未使用标签";
        }

        return $response;
    }



    /**
     * @author sally
     * @date   2020-05-10
     */
    private function _get_permission_flags()
    {
        $is_admin = false;
        $flags = [
            'delete_extra_labels' => false,
            'miscellaneous' => false,
        ];
        $permissions = UserPermissions::getPermissions(Yii::$app->user->identity->getId());

        foreach ($permissions as $permission) {
            $flags[$permission['permission_name']] = true;

            if ($permission['permission_name'] == 'full_access') {
                $is_admin = true;
            }
        }

        if ($is_admin) {
            foreach ($flags as $key => $flag) {
                $flags[$key] = true;
            }
        }

        return $flags;
    }
}
